package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.ValidateCodeService;
import com.itheima.untils.SMSUtils;
import com.itheima.untils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validatebank")
public class ValidateController {
    @Reference
    ValidateCodeService validateCodeService;
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/code")
    public Result getCode(String name) throws ClientException {
        String username = name;
        //设置一个四位数验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            //先判断是手机号还是用户名
            long phone = Long.parseLong(username);
            String telephone = String.valueOf(phone);
            //没报错说明是手机号 直接发送验证码
            //发送验证码
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            //将验证码保存到缓存中
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_GETPWD,300,code.toString());
            return new Result(true,"验证码已发送到您的手机");
        } catch (Exception e) {
            //说明是用户名  根据用户名查询数据库
            String telephone = validateCodeService.findByUsername(username);
            if (telephone==null){
                return new Result(false,"改用户名不存在");
            }
            //发送验证码
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            System.out.println(code);
            //将验证码保存到缓存中
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_GETPWD,300,code.toString());
            //截取前三个字符
            String begin = telephone.substring(0, 3);
            //截取手机号后4个字符
            String last = telephone.substring(telephone.length() - 4);
            return new Result(true,"验证码已发送到该用户对应的手机号为"+begin+"******"+last);
        }
    }
    /*
    * 核对验证码
    * */
    @RequestMapping("/checkCode")
    public Result checkCode(String name,String code){
        //判断验证码为空
        if (code==null){
            return new Result(false,"验证码不能为空");
        }
        try {
            //判断name是用户名或手机号
            Long telephone = Long.parseLong(name);
            //手机号
            //获取缓存中验证码
            String jedisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_GETPWD);
            if (!jedisCode.equals(code)){
                return new Result(false,"验证码不正确");
            }
            return new Result(true,"验证码正确");
        } catch (NumberFormatException e) {
            //说明是用户名
            String telephone = validateCodeService.findByUsername(name);
            //获取缓存中验证码
            String jedisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_GETPWD);
            if (!jedisCode.equals(code)){
                return new Result(false,"验证码不正确");
            }
            return new Result(true,"验证码正确");
        }
    }
}
